package com.wgworkshop.pems.controller;

import com.wgworkshop.pems.checkgroup.CreateCheckGroup;
import com.wgworkshop.pems.checkgroup.PatchCheckGroup;
import com.wgworkshop.pems.checkgroup.UpdateCheckGroup;
import com.wgworkshop.pems.common.ResponseData;
import com.wgworkshop.pems.model.StudentModel;
import com.wgworkshop.pems.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器。
 * Created by wlc on 2017/7/2.
 */
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping
    ResponseData<StudentModel> create(@RequestBody @Validated(CreateCheckGroup.class) StudentModel studentModel) {
        StudentModel resultModel = studentService.save(studentModel);
        return new ResponseData<StudentModel>().success(resultModel);
    }

    @DeleteMapping("/{id}")
    ResponseData<Void> delete(@PathVariable("id") Long id) {
        studentService.delete(id);
        return new ResponseData<Void>().success();
    }

    @PatchMapping("/{id}")
    ResponseData<StudentModel> update(@PathVariable("id") Long id,
                                      @RequestBody @Validated(PatchCheckGroup.class) StudentModel studentModel) {
        StudentModel resultModel = studentService.update(id, studentModel);
        return new ResponseData<StudentModel>().success(resultModel);
    }

    @PutMapping
    ResponseData<StudentModel> update(@RequestBody @Validated(UpdateCheckGroup.class) StudentModel studentModel) {
        StudentModel resultModel = studentService.update(studentModel);
        return new ResponseData<StudentModel>().success(resultModel);
    }

    @GetMapping("/{id}")
    ResponseData<StudentModel> findOne(@PathVariable("id") Long id) {
        StudentModel studentModel = studentService.findOne(id);
        return new ResponseData<StudentModel>().success(studentModel);
    }

    @GetMapping
    ResponseData<List<StudentModel>> findByNameContaining(@RequestParam(value = "name", required = false) String name) {
        List<StudentModel> studentModels = studentService.findByNameContaining(name);
        return new ResponseData<List<StudentModel>>().success(studentModels);
    }
}
